<template>
  <div class="app-container">
    <!-- 查询条件开始 -->
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="所属部门" prop="deptId">
        <el-select
            v-model="queryParams.deptId"
            placeholder="请选择所属部门"
            clearable
            size="small"
            style="width:200px">
          <el-option
              v-for="d in deptList"
              :key="d.deptId"
              :label="d.deptName"
              :value="d.deptId"/>
        </el-select>
      </el-form-item>
      <el-form-item label="患者名称" prop="patientName">
        <el-input
            v-model="queryParams.patientName"
            placeholder="请输入患者名称"
            clearable
            size="small"
            style="width:200px"/>
      </el-form-item>
      <el-form-item label="挂号类型" prop="schedulingType">
        <el-select
            v-model="queryParams.schedulingType"
            placeholder="可用状态"
            clearable
            size="small"
            style="width:200px">
          <el-option
              v-for="dict in schedulingTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="挂号时段" prop="subsectionType">
        <el-select
            v-model="queryParams.subsectionType"
            placeholder="挂号时段"
            clearable
            size="small"
            style="width:200px">
          <el-option
              v-for="dict in subsectionTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="挂号状态" prop="regStatus">
        <el-select
            v-model="queryParams.regStatus"
            placeholder="挂号状态"
            clearable
            size="small"
            style="width:200px">
          <el-option
              v-for="dict in regStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="挂号时间" prop="queryDate">
        <el-date-picker
            v-model="queryParams.queryDate"
            size="small"
            style="width:200px"
            value-format="yyyy-MM-dd"
            type="date"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="normal" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 数据表格开始 -->
    <el-table v-loading="loading" border :data="regTableList">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="right" inline class="demo-table-expand">
            <el-form-item label="挂号ID:">
              <span>{{ props.row.registrationId }}</span>
            </el-form-item>
            <el-form-item label="挂号员:">
              <span>{{ props.row.createBy }}</span>
            </el-form-item>
            <el-form-item label="创建时间:">
              <span>{{ props.row.createTime }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="患者姓名" align="center" prop="patientName"/>
      <el-table-column label="挂号科室" align="center" prop="deptId" :formatter="deptFormatter"/>
      <el-table-column label="接诊医生" align="center" prop="doctorName"/>
      <el-table-column label="挂号费用" align="center" prop="registrationAmount">
        <template slot-scope="scope">
          <span>{{ scope.row.registrationAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="流水编号" align="center" prop="registrationNumber"/>
      <el-table-column label="状态" align="center" prop="registrationStatus" :formatter="regStatusFormatter"/>
      <el-table-column label="就诊日期" align="center" prop="visitDate"/>
      <el-table-column label="挂号类型" align="center" prop="schedulingType" :formatter="schedulingTypeFormatter"/>
      <el-table-column label="挂号时段" align="center" prop="subsectionType" :formatter="subsectionTypeFormatter"/>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button v-if="scope.row.registrationStatus === '0'" type="success" icon="el-icon-check" size="mini"
                     @click="confirmAction(scope.row, 'collect')">收费
          </el-button>
          <el-button v-if="scope.row.registrationStatus === '1'" type="danger" icon="el-icon-close" size="mini"
                     @click="confirmAction(scope.row, 'return')">退号
          </el-button>
          <el-button v-if="scope.row.registrationStatus === '0'" type="danger" icon="el-icon-close" size="mini"
                     @click="confirmAction(scope.row, 'invalid')">作废
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 数据表格结束 -->
    <el-pagination
        :current="queryParams.pageNum"
        :page-sizes="[5, 10, 20, 30]"
        :pageSize="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryParams.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>

export default {
  name: "RegisteredList",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 挂号数据
      regTableList: [],
      // 部门数据
      deptList: [],
      // 用户数据
      userList: [],
      // 挂号单状态数据字典
      regStatusOptions: [],
      // 门诊急诊
      schedulingTypeOptions: [],
      // 上午  中午  晚上
      subsectionTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientName: '',
        deptId: '',
        schedulingType: '',
        subsectionType: '',
        visitDate: '',
        regStatus: '',
        userId: '',
        queryDate: new Date().toLocaleDateString('en-CA') // 默认设置为今天
      }
    }
  },
  created() {
    this.loaduser()
    this.loadData();

  },
  methods: {
    loadData() {

      this.loadDept();
      this.loadSchedulingTypes();
      this.loadSubsectionTypes();
      this.loadStatusOptions();
    },
    loaduser(){
      this.$http.get("/system/user/info").then(resp=>{
        this.userList = resp.data.data;
        this.loadUser();
      })
    },
    loadUser() {
      this.loading = true;
      this.queryParams.userId=this.userList.userId;
      this.$http.post("/doctor/registration/selectByPage", this.queryParams).then(resp => {
        console.log(resp.data.data);
        const {records, total} = resp.data.data;
        this.regTableList = records;

        this.queryParams.total = total;
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },
    // 分页pageSize变化时触发
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.loadUser();
    },
    // 点击上一页  下一页，跳转到哪一页面时触发
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.loadUser();
    },
    // 加载部门
    loadDept() {
      this.$http.get("/system/dept/list").then(resp => {
        this.deptList = resp.data.data;
      });
    },
    // 加载挂号单状态
    loadSchedulingTypes() {
      this.$http.get("/system/dict/data/type/his_scheduling_type").then(resp => {
        this.schedulingTypeOptions = resp.data.data;
      });
    },
    // 加载挂号时段
    loadSubsectionTypes() {
      this.$http.get("/system/dict/data/type/his_subsection_type").then(resp => {
        this.subsectionTypeOptions = resp.data.data;
      });
    },
    // 加载挂号单状态
    loadStatusOptions() {
      this.$http.get("/system/dict/data/type/his_registration_status").then(resp => {
        this.regStatusOptions = resp.data.data;
      });
    },
    // 格式化表格数据
    deptFormatter(row) {
      const dept = this.deptList.find(item => row.deptId === item.deptId);
      return dept ? dept.deptName : '未知';
    },
    regStatusFormatter(row) {
      const status = this.regStatusOptions.find(item => row.registrationStatus === item.dictValue);
      return status ? status.dictLabel : '未知';
    },
    schedulingTypeFormatter(row) {
      const type = this.schedulingTypeOptions.find(item => row.schedulingType === item.dictValue);
      return type ? type.dictLabel : '未知';
    },
    subsectionTypeFormatter(row) {
      const type = this.subsectionTypeOptions.find(item => row.subsectionType === item.dictValue);
      return type ? type.dictLabel : '未知';
    },
    // 查询
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.loadUser();
    },
    // 重置查询
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        patientName: '',
        deptId: '',
        schedulingType: '',
        subsectionType: '',
        visitDate: '',
        regStatus: '',
        queryDate: new Date().toLocaleDateString('en-CA')
      };
      this.loadUser();
    },
    // 显示确认对话框
    confirmAction(row, actionType) {
      let message = '';
      switch (actionType) {
        case 'collect':
          message = '确定要执行收费操作吗？';
          break;
        case 'return':
          message = '确定要执行退号操作吗？';
          break;
        case 'invalid':
          message = '确定要执行作废操作吗？';
          break;
        default:
          return;
      }
      this.$confirm(message, '确认操作', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.executeAction(row, actionType);
      }).catch(() => {
        this.$message.info('操作已取消');
      });
    },

    // 执行具体的操作
    executeAction(row, actionType) {
      switch (actionType) {
        case 'collect':
          this.handleCollect(row);
          break;
        case 'return':
          this.handleReturn(row);
          break;
        case 'invalid':
          this.handleInvalid(row);
          break;
        default:
          return;
      }
    },

    // 收费操作
    handleCollect(row) {
      this.updateRegistrationStatus(row.registrationId, '1', '收费成功');
    },

    // 退号操作
    handleReturn(row) {
      this.updateRegistrationStatus(row.registrationId, '4', '退号成功');
    },

    // 作废操作
    handleInvalid(row) {
      this.updateRegistrationStatus(row.registrationId, '5', '作废成功');
    },

    // 更新挂号状态
    updateRegistrationStatus(registrationId, status, successMessage) {
      this.$http.put("doctor/registration/updateStatus", {
        registrationId,
        registrationStatus: status
      }).then(response => {
        if (response.data.code === 200) {
          this.$message.success(successMessage);
          this.loadUser(); // 更新列表数据
        } else {
          this.$message.error(`操作失败: ${response.data.message}`);
        }
      }).catch(error => {
        this.$message.error(`请求错误: ${error}`);
      });
    }
  }
}
</script>


<style scoped>
.app-container {
  padding: 20px;
}
</style>
